package com.ruoyi.business.service.impl;

import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.*;


import com.ruoyi.business.domain.CarMain;
import com.ruoyi.business.domain.Cardetail;
import com.ruoyi.business.domain.Inventory;
import com.ruoyi.business.dto.DispatchListsExportDTO;
import com.ruoyi.common.core.domain.entity.SysUser;
import org.apache.commons.lang3.StringUtils;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.business.dao.DispatchListsDao;
import com.ruoyi.business.domain.DispatchLists;
import com.ruoyi.business.service.IDispatchListsService;

import javax.persistence.criteria.*;

/**
 * DispatchListsService业务层处理
 *
 * @author liutietou
 * @since 1.0 2025-07-19
 */
@Transactional(readOnly = true)
@Service
public class DispatchListsServiceImpl implements IDispatchListsService {

    @Autowired
    private DispatchListsDao dispatchListsDao;

    /**
     * 查询DispatchLists
     *
     * @param autoid DispatchListsID
     * @return DispatchLists
     */
    @Override
    public DispatchLists findById(Integer autoid) {
        return dispatchListsDao.findById(autoid).get();
    }

    /**
     * 分页查询DispatchLists列表
     *
     * @param req DispatchLists
     * @return DispatchLists
     */
    @Override
    public Page<DispatchLists> findDispatchListsPage(DispatchLists req) {
        Specification<DispatchLists> example = formatQueryParams(req);
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Pageable pageable = PageRequest.of(pageDomain.getPageNo(),
                Optional.ofNullable(pageDomain.getPageSize()).orElse(PageDomain.DEFAULT_PAGE_SIZE),
                Sort.Direction.ASC,
                Optional.ofNullable(pageDomain.getOrderByColumn()).orElse("autoid"));
        Page<DispatchLists> page = dispatchListsDao.findAll(example, pageable);
        return page;
    }

    /**
     * 分页查询DispatchLists列表
     *
     * @param req DispatchLists
     * @return DispatchLists
     */
    @Override
    public List<DispatchLists> findDispatchListsList(DispatchLists req) {
        Specification<DispatchLists> example = formatQueryParams(req);
        List<DispatchLists> list = dispatchListsDao.findAll(example, Sort.by(Sort.Direction.ASC,"autoid"));
        return list;
    }

    private Specification<DispatchLists> formatQueryParams(DispatchLists req){
        Specification<DispatchLists> example = new Specification<DispatchLists>() {
            private static final long serialVersionUID = 1L;
            @Override
            public Predicate toPredicate(Root<DispatchLists> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<>();
                if (null != req.getAutoid()){
                    Predicate pre = cb.equal(root.get("autoid").as(Integer.class), req.getAutoid());
                    list.add(pre);
                }
                if (null != req.getDlid()){
                    Predicate pre = cb.equal(root.get("dlid").as(Integer.class), req.getDlid());
                    list.add(pre);
                }else {
                    Predicate pre = cb.equal(root.get("dlid").as(Integer.class), -1);
                    list.add(pre);
                }
                if (null != req.getIcorid()){
                    Predicate pre = cb.equal(root.get("icorid").as(Integer.class), req.getIcorid());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCwhcode())){
                    Predicate pre = cb.equal(root.get("cwhcode").as(String.class), req.getCwhcode());
                    list.add(pre);
                }
                if (Objects.nonNull(req.getCinvcode())&& StringUtils.isNotBlank(req.getCinvcode().getCinvcode())){
                    Join<DispatchLists, Inventory>  inventoryJoin = root.join("cinvcode", JoinType.INNER);
                    Predicate pre = cb.equal(inventoryJoin.get("cinvcode").as(String.class), req.getCinvcode().getCinvcode());
                    list.add(pre);
                }
                if (null != req.getIquantity()){
                    Predicate pre = cb.equal(root.get("iquantity").as(Long.class), req.getIquantity());
                    list.add(pre);
                }
                if (null != req.getInum()){
                    Predicate pre = cb.equal(root.get("inum").as(Long.class), req.getInum());
                    list.add(pre);
                }
                if (null != req.getIquotedprice()){
                    Predicate pre = cb.equal(root.get("iquotedprice").as(Long.class), req.getIquotedprice());
                    list.add(pre);
                }
                if (null != req.getIunitprice()){
                    Predicate pre = cb.equal(root.get("iunitprice").as(Long.class), req.getIunitprice());
                    list.add(pre);
                }
                if (null != req.getItaxunitprice()){
                    Predicate pre = cb.equal(root.get("itaxunitprice").as(Long.class), req.getItaxunitprice());
                    list.add(pre);
                }
                if (null != req.getImoney()){
                    Predicate pre = cb.equal(root.get("imoney").as(BigDecimal.class), req.getImoney());
                    list.add(pre);
                }
                if (null != req.getItax()){
                    Predicate pre = cb.equal(root.get("itax").as(BigDecimal.class), req.getItax());
                    list.add(pre);
                }
                if (null != req.getIsum()){
                    Predicate pre = cb.equal(root.get("isum").as(BigDecimal.class), req.getIsum());
                    list.add(pre);
                }
                if (null != req.getIdiscount()){
                    Predicate pre = cb.equal(root.get("idiscount").as(BigDecimal.class), req.getIdiscount());
                    list.add(pre);
                }
                if (null != req.getInatunitprice()){
                    Predicate pre = cb.equal(root.get("inatunitprice").as(Long.class), req.getInatunitprice());
                    list.add(pre);
                }
                if (null != req.getInatmoney()){
                    Predicate pre = cb.equal(root.get("inatmoney").as(BigDecimal.class), req.getInatmoney());
                    list.add(pre);
                }
                if (null != req.getInattax()){
                    Predicate pre = cb.equal(root.get("inattax").as(BigDecimal.class), req.getInattax());
                    list.add(pre);
                }
                if (null != req.getInatsum()){
                    Predicate pre = cb.equal(root.get("inatsum").as(BigDecimal.class), req.getInatsum());
                    list.add(pre);
                }
                if (null != req.getInatdiscount()){
                    Predicate pre = cb.equal(root.get("inatdiscount").as(BigDecimal.class), req.getInatdiscount());
                    list.add(pre);
                }
                if (null != req.getIsettlenum()){
                    Predicate pre = cb.equal(root.get("isettlenum").as(Long.class), req.getIsettlenum());
                    list.add(pre);
                }
                if (null != req.getIsettlequantity()){
                    Predicate pre = cb.equal(root.get("isettlequantity").as(Long.class), req.getIsettlequantity());
                    list.add(pre);
                }
                if (null != req.getIbatch()){
                    Predicate pre = cb.equal(root.get("ibatch").as(Integer.class), req.getIbatch());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCbatch())){
                    Predicate pre = cb.equal(root.get("cbatch").as(String.class), req.getCbatch());
                    list.add(pre);
                }
                if (null != req.getBsettleall()){
                    Predicate pre = cb.equal(root.get("bsettleall").as(Integer.class), req.getBsettleall());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCmemo())){
                    Predicate pre = cb.equal(root.get("cmemo").as(String.class), req.getCmemo());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCfree1())){
                    Predicate pre = cb.equal(root.get("cfree1").as(String.class), req.getCfree1());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCfree2())){
                    Predicate pre = cb.equal(root.get("cfree2").as(String.class), req.getCfree2());
                    list.add(pre);
                }
                if (null != req.getItb()){
                    Predicate pre = cb.equal(root.get("itb").as(Integer.class), req.getItb());
                    list.add(pre);
                }
                if (null != req.getDvdate()){
                    Predicate pre = cb.equal(root.get("dvdate").as(Date.class), req.getDvdate());
                    list.add(pre);
                }
                if (null != req.getTbquantity()){
                    Predicate pre = cb.equal(root.get("tbquantity").as(BigDecimal.class), req.getTbquantity());
                    list.add(pre);
                }
                if (null != req.getTbnum()){
                    Predicate pre = cb.equal(root.get("tbnum").as(BigDecimal.class), req.getTbnum());
                    list.add(pre);
                }
                if (null != req.getIsosid()){
                    Predicate pre = cb.equal(root.get("isosid").as(Integer.class), req.getIsosid());
                    list.add(pre);
                }
                if (null != req.getIdlsid()){
                    Predicate pre = cb.equal(root.get("idlsid").as(Integer.class), req.getIdlsid());
                    list.add(pre);
                }
                if (null != req.getKl()){
                    Predicate pre = cb.equal(root.get("kl").as(Long.class), req.getKl());
                    list.add(pre);
                }
                if (null != req.getKl2()){
                    Predicate pre = cb.equal(root.get("kl2").as(Long.class), req.getKl2());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCinvname())){
                    Predicate pre = cb.like(root.get("cinvname").as(String.class), "%" + req.getCinvname()+ "%");
                    list.add(pre);
                }
                if (null != req.getItaxrate()){
                    Predicate pre = cb.equal(root.get("itaxrate").as(Long.class), req.getItaxrate());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCdefine22())){
                    Predicate pre = cb.equal(root.get("cdefine22").as(String.class), req.getCdefine22());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCdefine23())){
                    Predicate pre = cb.equal(root.get("cdefine23").as(String.class), req.getCdefine23());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCdefine24())){
                    Predicate pre = cb.equal(root.get("cdefine24").as(String.class), req.getCdefine24());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCdefine25())){
                    Predicate pre = cb.equal(root.get("cdefine25").as(String.class), req.getCdefine25());
                    list.add(pre);
                }
                if (null != req.getCdefine26()){
                    Predicate pre = cb.equal(root.get("cdefine26").as(Long.class), req.getCdefine26());
                    list.add(pre);
                }
                if (null != req.getCdefine27()){
                    Predicate pre = cb.equal(root.get("cdefine27").as(Long.class), req.getCdefine27());
                    list.add(pre);
                }
                if (null != req.getFoutquantity()){
                    Predicate pre = cb.equal(root.get("foutquantity").as(Long.class), req.getFoutquantity());
                    list.add(pre);
                }
                if (null != req.getFoutnum()){
                    Predicate pre = cb.equal(root.get("foutnum").as(Long.class), req.getFoutnum());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCitemcode())){
                    Predicate pre = cb.equal(root.get("citemcode").as(String.class), req.getCitemcode());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCitemClass())){
                    Predicate pre = cb.equal(root.get("citemClass").as(String.class), req.getCitemClass());
                    list.add(pre);
                }
                if (null != req.getFsalecost()){
                    Predicate pre = cb.equal(root.get("fsalecost").as(Long.class), req.getFsalecost());
                    list.add(pre);
                }
                if (null != req.getFsaleprice()){
                    Predicate pre = cb.equal(root.get("fsaleprice").as(Long.class), req.getFsaleprice());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCvenabbname())){
                    Predicate pre = cb.like(root.get("cvenabbname").as(String.class), "%" + req.getCvenabbname()+ "%");
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCitemname())){
                    Predicate pre = cb.like(root.get("citemname").as(String.class), "%" + req.getCitemname()+ "%");
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCitemCname())){
                    Predicate pre = cb.like(root.get("citemCname").as(String.class), "%" + req.getCitemCname()+ "%");
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCfree3())){
                    Predicate pre = cb.equal(root.get("cfree3").as(String.class), req.getCfree3());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCfree4())){
                    Predicate pre = cb.equal(root.get("cfree4").as(String.class), req.getCfree4());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCfree5())){
                    Predicate pre = cb.equal(root.get("cfree5").as(String.class), req.getCfree5());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCfree6())){
                    Predicate pre = cb.equal(root.get("cfree6").as(String.class), req.getCfree6());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCfree7())){
                    Predicate pre = cb.equal(root.get("cfree7").as(String.class), req.getCfree7());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCfree8())){
                    Predicate pre = cb.equal(root.get("cfree8").as(String.class), req.getCfree8());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCfree9())){
                    Predicate pre = cb.equal(root.get("cfree9").as(String.class), req.getCfree9());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCfree10())){
                    Predicate pre = cb.equal(root.get("cfree10").as(String.class), req.getCfree10());
                    list.add(pre);
                }
                if (null != req.getBisstqc()){
                    Predicate pre = cb.equal(root.get("bisstqc").as(Integer.class), req.getBisstqc());
                    list.add(pre);
                }
                if (null != req.getIinvexchrate()){
                    Predicate pre = cb.equal(root.get("iinvexchrate").as(BigDecimal.class), req.getIinvexchrate());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCunitid())){
                    Predicate pre = cb.equal(root.get("cunitid").as(String.class), req.getCunitid());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCcode())){
                    Predicate pre = cb.equal(root.get("ccode").as(String.class), req.getCcode());
                    list.add(pre);
                }
                if (null != req.getIretquantity()){
                    Predicate pre = cb.equal(root.get("iretquantity").as(BigDecimal.class), req.getIretquantity());
                    list.add(pre);
                }
                if (null != req.getFensettlequan()){
                    Predicate pre = cb.equal(root.get("fensettlequan").as(BigDecimal.class), req.getFensettlequan());
                    list.add(pre);
                }
                if (null != req.getFensettlesum()){
                    Predicate pre = cb.equal(root.get("fensettlesum").as(BigDecimal.class), req.getFensettlesum());
                    list.add(pre);
                }
                if (null != req.getIsettleprice()){
                    Predicate pre = cb.equal(root.get("isettleprice").as(Long.class), req.getIsettleprice());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCdefine28())){
                    Predicate pre = cb.equal(root.get("cdefine28").as(String.class), req.getCdefine28());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCdefine29())){
                    Predicate pre = cb.equal(root.get("cdefine29").as(String.class), req.getCdefine29());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCdefine30())){
                    Predicate pre = cb.equal(root.get("cdefine30").as(String.class), req.getCdefine30());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCdefine31())){
                    Predicate pre = cb.equal(root.get("cdefine31").as(String.class), req.getCdefine31());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCdefine32())){
                    Predicate pre = cb.equal(root.get("cdefine32").as(String.class), req.getCdefine32());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCdefine33())){
                    Predicate pre = cb.equal(root.get("cdefine33").as(String.class), req.getCdefine33());
                    list.add(pre);
                }
                if (null != req.getCdefine34()){
                    Predicate pre = cb.equal(root.get("cdefine34").as(Integer.class), req.getCdefine34());
                    list.add(pre);
                }
                if (null != req.getCdefine35()){
                    Predicate pre = cb.equal(root.get("cdefine35").as(Integer.class), req.getCdefine35());
                    list.add(pre);
                }
                if (null != req.getCdefine36()){
                    Predicate pre = cb.equal(root.get("cdefine36").as(Date.class), req.getCdefine36());
                    list.add(pre);
                }
                if (null != req.getCdefine37()){
                    Predicate pre = cb.equal(root.get("cdefine37").as(Date.class), req.getCdefine37());
                    list.add(pre);
                }
                if (null != req.getDmdate()){
                    Predicate pre = cb.equal(root.get("dmdate").as(Date.class), req.getDmdate());
                    list.add(pre);
                }
                if (null != req.getBgsp()){
                    Predicate pre = cb.equal(root.get("bgsp").as(Integer.class), req.getBgsp());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCgspstate())){
                    Predicate pre = cb.equal(root.get("cgspstate").as(String.class), req.getCgspstate());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCsocode())){
                    Predicate pre = cb.equal(root.get("csocode").as(String.class), req.getCsocode());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCcorcode())){
                    Predicate pre = cb.equal(root.get("ccorcode").as(String.class), req.getCcorcode());
                    list.add(pre);
                }
                if (null != req.getIppartseqid()){
                    Predicate pre = cb.equal(root.get("ippartseqid").as(Integer.class), req.getIppartseqid());
                    list.add(pre);
                }
                if (null != req.getIppartid()){
                    Predicate pre = cb.equal(root.get("ippartid").as(Integer.class), req.getIppartid());
                    list.add(pre);
                }
                if (null != req.getIppartqty()){
                    Predicate pre = cb.equal(root.get("ippartqty").as(BigDecimal.class), req.getIppartqty());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCcontractid())){
                    Predicate pre = cb.equal(root.get("ccontractid").as(String.class), req.getCcontractid());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCcontracttagcode())){
                    Predicate pre = cb.equal(root.get("ccontracttagcode").as(String.class), req.getCcontracttagcode());
                    list.add(pre);
                }
                if (null != req.getCcontractrowguid()){
                    Predicate pre = cb.equal(root.get("ccontractrowguid").as(BigDecimal.class), req.getCcontractrowguid());
                    list.add(pre);
                }
                if (null != req.getImassdate()){
                    Predicate pre = cb.equal(root.get("imassdate").as(Integer.class), req.getImassdate());
                    list.add(pre);
                }
                if (null != req.getCmassunit()){
                    Predicate pre = cb.equal(root.get("cmassunit").as(Integer.class), req.getCmassunit());
                    list.add(pre);
                }
                if (null != req.getBqaneedcheck()){
                    Predicate pre = cb.equal(root.get("bqaneedcheck").as(Integer.class), req.getBqaneedcheck());
                    list.add(pre);
                }
                if (null != req.getBqaurgency()){
                    Predicate pre = cb.equal(root.get("bqaurgency").as(Integer.class), req.getBqaurgency());
                    list.add(pre);
                }
                if (null != req.getBqachecking()){
                    Predicate pre = cb.equal(root.get("bqachecking").as(Integer.class), req.getBqachecking());
                    list.add(pre);
                }
                if (null != req.getBqachecked()){
                    Predicate pre = cb.equal(root.get("bqachecked").as(Integer.class), req.getBqachecked());
                    list.add(pre);
                }
                if (null != req.getIqaquantity()){
                    Predicate pre = cb.equal(root.get("iqaquantity").as(Long.class), req.getIqaquantity());
                    list.add(pre);
                }
                if (null != req.getIqanum()){
                    Predicate pre = cb.equal(root.get("iqanum").as(Long.class), req.getIqanum());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCcusinvcode())){
                    Predicate pre = cb.equal(root.get("ccusinvcode").as(String.class), req.getCcusinvcode());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCcusinvname())){
                    Predicate pre = cb.like(root.get("ccusinvname").as(String.class), "%" + req.getCcusinvname()+ "%");
                    list.add(pre);
                }
                if (null != req.getFsumsignquantity()){
                    Predicate pre = cb.equal(root.get("fsumsignquantity").as(BigDecimal.class), req.getFsumsignquantity());
                    list.add(pre);
                }
                if (null != req.getFsumsignnum()){
                    Predicate pre = cb.equal(root.get("fsumsignnum").as(BigDecimal.class), req.getFsumsignnum());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCbaccounter())){
                    Predicate pre = cb.equal(root.get("cbaccounter").as(String.class), req.getCbaccounter());
                    list.add(pre);
                }
                if (null != req.getBcosting()){
                    Predicate pre = cb.equal(root.get("bcosting").as(Integer.class), req.getBcosting());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCordercode())){
                    Predicate pre = cb.equal(root.get("cordercode").as(String.class), req.getCordercode());
                    list.add(pre);
                }
                if (null != req.getIorderrowno()){
                    Predicate pre = cb.equal(root.get("iorderrowno").as(Long.class), req.getIorderrowno());
                    list.add(pre);
                }
                if (null != req.getFcusminprice()){
                    Predicate pre = cb.equal(root.get("fcusminprice").as(BigDecimal.class), req.getFcusminprice());
                    list.add(pre);
                }
                if (null != req.getIcostquantity()){
                    Predicate pre = cb.equal(root.get("icostquantity").as(BigDecimal.class), req.getIcostquantity());
                    list.add(pre);
                }
                if (null != req.getIcostsum()){
                    Predicate pre = cb.equal(root.get("icostsum").as(BigDecimal.class), req.getIcostsum());
                    list.add(pre);
                }
                if (null != req.getIspecialtype()){
                    Predicate pre = cb.equal(root.get("ispecialtype").as(Integer.class), req.getIspecialtype());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCvmivencode())){
                    Predicate pre = cb.equal(root.get("cvmivencode").as(String.class), req.getCvmivencode());
                    list.add(pre);
                }
                if (null != req.getIexchsum()){
                    Predicate pre = cb.equal(root.get("iexchsum").as(BigDecimal.class), req.getIexchsum());
                    list.add(pre);
                }
                if (null != req.getImoneysum()){
                    Predicate pre = cb.equal(root.get("imoneysum").as(BigDecimal.class), req.getImoneysum());
                    list.add(pre);
                }
                if (null != req.getIrowno()){
                    Predicate pre = cb.equal(root.get("irowno").as(Long.class), req.getIrowno());
                    list.add(pre);
                }
                if (null != req.getFrettbquantity()){
                    Predicate pre = cb.equal(root.get("frettbquantity").as(BigDecimal.class), req.getFrettbquantity());
                    list.add(pre);
                }
                if (null != req.getFretsum()){
                    Predicate pre = cb.equal(root.get("fretsum").as(BigDecimal.class), req.getFretsum());
                    list.add(pre);
                }
                if (null != req.getIexpiratdatecalcu()){
                    Predicate pre = cb.equal(root.get("iexpiratdatecalcu").as(Integer.class), req.getIexpiratdatecalcu());
                    list.add(pre);
                }
                if (null != req.getDexpirationdate()){
                    Predicate pre = cb.equal(root.get("dexpirationdate").as(Date.class), req.getDexpirationdate());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCexpirationdate())){
                    Predicate pre = cb.equal(root.get("cexpirationdate").as(String.class), req.getCexpirationdate());
                    list.add(pre);
                }
                if (null != req.getCbatchproperty1()){
                    Predicate pre = cb.equal(root.get("cbatchproperty1").as(BigDecimal.class), req.getCbatchproperty1());
                    list.add(pre);
                }
                if (null != req.getCbatchproperty2()){
                    Predicate pre = cb.equal(root.get("cbatchproperty2").as(BigDecimal.class), req.getCbatchproperty2());
                    list.add(pre);
                }
                if (null != req.getCbatchproperty3()){
                    Predicate pre = cb.equal(root.get("cbatchproperty3").as(BigDecimal.class), req.getCbatchproperty3());
                    list.add(pre);
                }
                if (null != req.getCbatchproperty4()){
                    Predicate pre = cb.equal(root.get("cbatchproperty4").as(BigDecimal.class), req.getCbatchproperty4());
                    list.add(pre);
                }
                if (null != req.getCbatchproperty5()){
                    Predicate pre = cb.equal(root.get("cbatchproperty5").as(BigDecimal.class), req.getCbatchproperty5());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCbatchproperty6())){
                    Predicate pre = cb.equal(root.get("cbatchproperty6").as(String.class), req.getCbatchproperty6());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCbatchproperty7())){
                    Predicate pre = cb.equal(root.get("cbatchproperty7").as(String.class), req.getCbatchproperty7());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCbatchproperty8())){
                    Predicate pre = cb.equal(root.get("cbatchproperty8").as(String.class), req.getCbatchproperty8());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCbatchproperty9())){
                    Predicate pre = cb.equal(root.get("cbatchproperty9").as(String.class), req.getCbatchproperty9());
                    list.add(pre);
                }
                if (null != req.getCbatchproperty10()){
                    Predicate pre = cb.equal(root.get("cbatchproperty10").as(Date.class), req.getCbatchproperty10());
                    list.add(pre);
                }
                if (null != req.getDblpreexchmomey()){
                    Predicate pre = cb.equal(root.get("dblpreexchmomey").as(BigDecimal.class), req.getDblpreexchmomey());
                    list.add(pre);
                }
                if (null != req.getDblpremomey()){
                    Predicate pre = cb.equal(root.get("dblpremomey").as(BigDecimal.class), req.getDblpremomey());
                    list.add(pre);
                }
                if (null != req.getIdemandtype()){
                    Predicate pre = cb.equal(root.get("idemandtype").as(Integer.class), req.getIdemandtype());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCdemandcode())){
                    Predicate pre = cb.equal(root.get("cdemandcode").as(String.class), req.getCdemandcode());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCdemandmemo())){
                    Predicate pre = cb.equal(root.get("cdemandmemo").as(String.class), req.getCdemandmemo());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCdemandid())){
                    Predicate pre = cb.equal(root.get("cdemandid").as(String.class), req.getCdemandid());
                    list.add(pre);
                }
                if (null != req.getIdemandseq()){
                    Predicate pre = cb.equal(root.get("idemandseq").as(Long.class), req.getIdemandseq());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCvencode())){
                    Predicate pre = cb.equal(root.get("cvencode").as(String.class), req.getCvencode());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCreasoncode())){
                    Predicate pre = cb.equal(root.get("creasoncode").as(String.class), req.getCreasoncode());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCinvsn())){
                    Predicate pre = cb.equal(root.get("cinvsn").as(String.class), req.getCinvsn());
                    list.add(pre);
                }
                if (null != req.getIinvsncount()){
                    Predicate pre = cb.equal(root.get("iinvsncount").as(Integer.class), req.getIinvsncount());
                    list.add(pre);
                }
                if (null != req.getBneedsign()){
                    Predicate pre = cb.equal(root.get("bneedsign").as(Integer.class), req.getBneedsign());
                    list.add(pre);
                }
                if (null != req.getBsignover()){
                    Predicate pre = cb.equal(root.get("bsignover").as(Integer.class), req.getBsignover());
                    list.add(pre);
                }
                if (null != req.getBneedloss()){
                    Predicate pre = cb.equal(root.get("bneedloss").as(Integer.class), req.getBneedloss());
                    list.add(pre);
                }
                if (null != req.getFlossrate()){
                    Predicate pre = cb.equal(root.get("flossrate").as(BigDecimal.class), req.getFlossrate());
                    list.add(pre);
                }
                if (null != req.getFrlossqty()){
                    Predicate pre = cb.equal(root.get("frlossqty").as(BigDecimal.class), req.getFrlossqty());
                    list.add(pre);
                }
                if (null != req.getFulossqty()){
                    Predicate pre = cb.equal(root.get("fulossqty").as(BigDecimal.class), req.getFulossqty());
                    list.add(pre);
                }
                if (null != req.getIsettletype()){
                    Predicate pre = cb.equal(root.get("isettletype").as(Integer.class), req.getIsettletype());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCrelacuscode())){
                    Predicate pre = cb.equal(root.get("crelacuscode").as(String.class), req.getCrelacuscode());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getClossmaker())){
                    Predicate pre = cb.equal(root.get("clossmaker").as(String.class), req.getClossmaker());
                    list.add(pre);
                }
                if (null != req.getDlossdate()){
                    Predicate pre = cb.equal(root.get("dlossdate").as(Date.class), req.getDlossdate());
                    list.add(pre);
                }
                if (null != req.getDlosstime()){
                    Predicate pre = cb.equal(root.get("dlosstime").as(Date.class), req.getDlosstime());
                    list.add(pre);
                }
                if (null != req.getIcoridlsid()){
                    Predicate pre = cb.equal(root.get("icoridlsid").as(Long.class), req.getIcoridlsid());
                    list.add(pre);
                }
                if (null != req.getFretoutqty()){
                    Predicate pre = cb.equal(root.get("fretoutqty").as(BigDecimal.class), req.getFretoutqty());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getBodyOutid())){
                    Predicate pre = cb.equal(root.get("bodyOutid").as(String.class), req.getBodyOutid());
                    list.add(pre);
                }
                if (null != req.getFveribillqty()){
                    Predicate pre = cb.equal(root.get("fveribillqty").as(BigDecimal.class), req.getFveribillqty());
                    list.add(pre);
                }
                if (null != req.getFveribillsum()){
                    Predicate pre = cb.equal(root.get("fveribillsum").as(BigDecimal.class), req.getFveribillsum());
                    list.add(pre);
                }
                if (null != req.getFveriretqty()){
                    Predicate pre = cb.equal(root.get("fveriretqty").as(BigDecimal.class), req.getFveriretqty());
                    list.add(pre);
                }
                if (null != req.getFveriretsum()){
                    Predicate pre = cb.equal(root.get("fveriretsum").as(BigDecimal.class), req.getFveriretsum());
                    list.add(pre);
                }
                if (null != req.getFlastsettleqty()){
                    Predicate pre = cb.equal(root.get("flastsettleqty").as(BigDecimal.class), req.getFlastsettleqty());
                    list.add(pre);
                }
                if (null != req.getFlastsettlesum()){
                    Predicate pre = cb.equal(root.get("flastsettlesum").as(BigDecimal.class), req.getFlastsettlesum());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCbookwhcode())){
                    Predicate pre = cb.equal(root.get("cbookwhcode").as(String.class), req.getCbookwhcode());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCinvouchtype())){
                    Predicate pre = cb.equal(root.get("cinvouchtype").as(String.class), req.getCinvouchtype());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCposition())){
                    Predicate pre = cb.equal(root.get("cposition").as(String.class), req.getCposition());
                    list.add(pre);
                }
                if (null != req.getFretqtywkp()){
                    Predicate pre = cb.equal(root.get("fretqtywkp").as(BigDecimal.class), req.getFretqtywkp());
                    list.add(pre);
                }
                if (null != req.getFretqtyykp()){
                    Predicate pre = cb.equal(root.get("fretqtyykp").as(BigDecimal.class), req.getFretqtyykp());
                    list.add(pre);
                }
                if (null != req.getFrettbqtyykp()){
                    Predicate pre = cb.equal(root.get("frettbqtyykp").as(BigDecimal.class), req.getFrettbqtyykp());
                    list.add(pre);
                }
                if (null != req.getFretsumykp()){
                    Predicate pre = cb.equal(root.get("fretsumykp").as(BigDecimal.class), req.getFretsumykp());
                    list.add(pre);
                }
                if (null != req.getDkeepdate()){
                    Predicate pre = cb.equal(root.get("dkeepdate").as(Date.class), req.getDkeepdate());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCscloser())){
                    Predicate pre = cb.equal(root.get("cscloser").as(String.class), req.getCscloser());
                    list.add(pre);
                }
                if (null != req.getIsaleoutid()){
                    Predicate pre = cb.equal(root.get("isaleoutid").as(Long.class), req.getIsaleoutid());
                    list.add(pre);
                }
                if (null != req.getBsaleprice()){
                    Predicate pre = cb.equal(root.get("bsaleprice").as(Integer.class), req.getBsaleprice());
                    list.add(pre);
                }
                if (null != req.getBgift()){
                    Predicate pre = cb.equal(root.get("bgift").as(Integer.class), req.getBgift());
                    list.add(pre);
                }
                if (null != req.getBmpforderclosed()){
                    Predicate pre = cb.equal(root.get("bmpforderclosed").as(Integer.class), req.getBmpforderclosed());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCbsysbarcode())){
                    Predicate pre = cb.equal(root.get("cbsysbarcode").as(String.class), req.getCbsysbarcode());
                    list.add(pre);
                }
                if (null != req.getFxjquantity()){
                    Predicate pre = cb.equal(root.get("fxjquantity").as(Long.class), req.getFxjquantity());
                    list.add(pre);
                }
                if (null != req.getFxjnum()){
                    Predicate pre = cb.equal(root.get("fxjnum").as(Long.class), req.getFxjnum());
                    list.add(pre);
                }
                if (null != req.getBiacreatebill()){
                    Predicate pre = cb.equal(root.get("biacreatebill").as(Integer.class), req.getBiacreatebill());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCparentcode())){
                    Predicate pre = cb.equal(root.get("cparentcode").as(String.class), req.getCparentcode());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCchildcode())){
                    Predicate pre = cb.equal(root.get("cchildcode").as(String.class), req.getCchildcode());
                    list.add(pre);
                }
                if (null != req.getFchildqty()){
                    Predicate pre = cb.equal(root.get("fchildqty").as(BigDecimal.class), req.getFchildqty());
                    list.add(pre);
                }
                if (null != req.getFchildrate()){
                    Predicate pre = cb.equal(root.get("fchildrate").as(BigDecimal.class), req.getFchildrate());
                    list.add(pre);
                }
                if (null != req.getIcalctype()){
                    Predicate pre = cb.equal(root.get("icalctype").as(Integer.class), req.getIcalctype());
                    list.add(pre);
                }
                if (null != req.getFappretwkpqty()){
                    Predicate pre = cb.equal(root.get("fappretwkpqty").as(BigDecimal.class), req.getFappretwkpqty());
                    list.add(pre);
                }
                if (null != req.getFappretwkpsum()){
                    Predicate pre = cb.equal(root.get("fappretwkpsum").as(BigDecimal.class), req.getFappretwkpsum());
                    list.add(pre);
                }
                if (null != req.getFappretykpqty()){
                    Predicate pre = cb.equal(root.get("fappretykpqty").as(BigDecimal.class), req.getFappretykpqty());
                    list.add(pre);
                }
                if (null != req.getFappretykpsum()){
                    Predicate pre = cb.equal(root.get("fappretykpsum").as(BigDecimal.class), req.getFappretykpsum());
                    list.add(pre);
                }
                if (null != req.getFappretwkptbqty()){
                    Predicate pre = cb.equal(root.get("fappretwkptbqty").as(BigDecimal.class), req.getFappretwkptbqty());
                    list.add(pre);
                }
                if (null != req.getFappretykptbqty()){
                    Predicate pre = cb.equal(root.get("fappretykptbqty").as(BigDecimal.class), req.getFappretykptbqty());
                    list.add(pre);
                }
                if (null != req.getIrtnappid()){
                    Predicate pre = cb.equal(root.get("irtnappid").as(Long.class), req.getIrtnappid());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCrtnappcode())){
                    Predicate pre = cb.equal(root.get("crtnappcode").as(String.class), req.getCrtnappcode());
                    list.add(pre);
                }
                if (null != req.getFretailrealamount()){
                    Predicate pre = cb.equal(root.get("fretailrealamount").as(BigDecimal.class), req.getFretailrealamount());
                    list.add(pre);
                }
                if (null != req.getFretailsettleamount()){
                    Predicate pre = cb.equal(root.get("fretailsettleamount").as(BigDecimal.class), req.getFretailsettleamount());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCfactorycode())){
                    Predicate pre = cb.equal(root.get("cfactorycode").as(String.class), req.getCfactorycode());
                    list.add(pre);
                }
                if (null != req.getGcsourceid()){
                    Predicate pre = cb.equal(root.get("gcsourceid").as(Integer.class), req.getGcsourceid());
                    list.add(pre);
                }
                if (null != req.getGcsourceids()){
                    Predicate pre = cb.equal(root.get("gcsourceids").as(Integer.class), req.getGcsourceids());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCconfirmer())){
                    Predicate pre = cb.equal(root.get("cconfirmer").as(String.class), req.getCconfirmer());
                    list.add(pre);
                }
                if (null != req.getDconfirmdate()){
                    Predicate pre = cb.equal(root.get("dconfirmdate").as(Date.class), req.getDconfirmdate());
                    list.add(pre);
                }
                if (null != req.getDsclosedate()){
                    Predicate pre = cb.equal(root.get("dsclosedate").as(Date.class), req.getDsclosedate());
                    list.add(pre);
                }
                if (null != req.getDsclosesystime()){
                    Predicate pre = cb.equal(root.get("dsclosesystime").as(Date.class), req.getDsclosesystime());
                    list.add(pre);
                }
                if (list.isEmpty()) {
                    return null;
                }
                return cb.and(list.toArray(new Predicate[0]));
            }
        };
        return example;
    }

    /**
     * 保存（新增/修改）DispatchLists
     *
     * @param dispatchLists DispatchLists
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(DispatchLists dispatchLists) {
        dispatchListsDao.save(dispatchLists);
    }



    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(List<DispatchLists> list,int dlid) {
     this.dispatchListsDao.deleteByDlid(dlid);
     int  dlsid=   getAndUpdateIdlsid("001");
      for(DispatchLists dispatchLists:list){
          dispatchLists.setIdlsid(dlsid++);
      }
        this.dispatchListsDao.updateDispatchsId("001",dlsid);
        dispatchListsDao.saveAll(list);
    }

    /**
     * 批量删除DispatchLists
     *
     * @param autoids 需要删除的DispatchListsID
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteByIds(List<Integer> autoids) {
        List<DispatchLists> existBeans = dispatchListsDao.findAllById(autoids);
        if(!existBeans.isEmpty()){
            dispatchListsDao.deleteAll(existBeans);
        }
    }

    private  int  getAndUpdateIdlsid(String accountid){
        int  idlsid=this.dispatchListsDao.getDispatchsId(accountid)+110*1000000;
        return  idlsid;
    }


    /**
     * 删除DispatchLists信息
     *
     * @param autoid DispatchListsID
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteDispatchListsById(Integer autoid) {
         dispatchListsDao.deleteById(autoid);
    }

    /**
     * 导出PDF
     *
     * @param exportDTO 导出参数
     * @param outputStream 输出流
     */
    @Override
    public void exportPDF(DispatchListsExportDTO exportDTO, OutputStream outputStream) {
        try {
            // 使用简单的文本格式生成PDF内容
            // 这里先实现一个简单的版本，后续可以集成iText等PDF库
            
            StringBuilder content = new StringBuilder();
            content.append("调度单明细表\n\n");
            content.append("发货单号：").append(exportDTO.getCdlcode() != null ? exportDTO.getCdlcode() : "").append("\n");
            content.append("日期：").append(exportDTO.getDdate() != null ? exportDTO.getDdate() : "").append("\n");
            content.append("客户编码：").append(exportDTO.getCcuscode() != null ? exportDTO.getCcuscode() : "").append("\n");
            content.append("客户名称：").append(exportDTO.getCcusname() != null ? exportDTO.getCcusname() : "").append("\n");
            content.append("联系人：").append(exportDTO.getCcusperson() != null ? exportDTO.getCcusperson() : "").append("\n");
            content.append("联系手机：").append(exportDTO.getCmobilephone() != null ? exportDTO.getCmobilephone() : "").append("\n");
            content.append("收货地址：").append(exportDTO.getCshipaddress() != null ? exportDTO.getCshipaddress() : "").append("\n");
            content.append("填制人：").append(exportDTO.getCmodifier() != null ? exportDTO.getCmodifier() : "").append("\n");
            content.append("审核人：").append(exportDTO.getCverifier() != null ? exportDTO.getCverifier() : "").append("\n");
            content.append("备注：").append(exportDTO.getCmemo() != null ? exportDTO.getCmemo() : "").append("\n\n");
            
            content.append("序号\t产品编码\t产品名称\t规格型号\t数量\t报价\t折扣\t含税价\t无税金额\t税额\t价税合计\t税率\t备注\n");
            
            if (exportDTO.getDispatchLists() != null) {
                for (int i = 0; i < exportDTO.getDispatchLists().size(); i++) {
                    DispatchLists item = exportDTO.getDispatchLists().get(i);
                    content.append(i + 1).append("\t");
                    content.append(item.getCinvcode() != null ? item.getCinvcode().getCinvcode() : "").append("\t");
                    content.append(item.getCinvcode() != null ? item.getCinvcode().getCinvname() : "").append("\t");
                    content.append(item.getCinvcode() != null ? item.getCinvcode().getCinvstd() : "").append("\t");
                    content.append(item.getIquantity() != null ? item.getIquantity() : "0").append("\t");
                    content.append(item.getIquotedprice() != null ? item.getIquotedprice() : "0").append("\t");
                    content.append(item.getKl() != null ? item.getKl() : "100").append("\t");
                    content.append(item.getItaxunitprice() != null ? item.getItaxunitprice() : "0").append("\t");
                    content.append(item.getImoney() != null ? item.getImoney() : "0").append("\t");
                    content.append(item.getItax() != null ? item.getItax() : "0").append("\t");
                    content.append(item.getIsum() != null ? item.getIsum() : "0").append("\t");
                    content.append(item.getItaxrate() != null ? item.getItaxrate() : "0").append("\t");
                    content.append(item.getCmemo() != null ? item.getCmemo() : "").append("\n");
                }
            }
            
            content.append("\n总数量：").append(exportDTO.getDispatchLists() != null ? exportDTO.getDispatchLists().size() : 0);
            
            // 将内容写入输出流
            outputStream.write(content.toString().getBytes("UTF-8"));
            outputStream.flush();
            
        } catch (Exception e) {
            throw new RuntimeException("PDF生成失败：" + e.getMessage(), e);
        }
    }
}
